Passed
Push — main ( b7799b...6c7a2f )
by Bjarn
03:09 queued 01:45
created

XdebugController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B execute 0 23 5
A enable 0 12 2
A disable 0 14 3
1
import Xdebug from '../extensions/php/xdebug'
2
import {getLinkedPhpVersion} from '../utils/phpFpm'
3
4
class XdebugController {
5
6
    /**
7
     * Switch the service to the given version.
8
     */
9
    execute = async (status: string): Promise<boolean> => {
10
        if (status !== 'on' && status !== 'off') {
11
            console.log(`Invalid status. Please provide status 'on' or 'off'.`)
12
            return false
13
        }
14
15
        const xdebug = new Xdebug()
16
        let restart = false
17
18
        if (status === 'on') {
19
            restart = await this.enable(xdebug)
20
        }
21
22
        if (status === 'off') {
23
            restart = await this.disable(xdebug)
24
        }
25
26
        if (restart) {
27
            const php = await getLinkedPhpVersion()
28
            await php.restart()
29
        }
30
31
        return true
32
    }
33
34
    enable = async (xdebug: Xdebug): Promise<boolean> => {
35
        if (!(await xdebug.isInstalled())) {
36
            console.log('Extension xdebug is not installed. Installing now...')
37
            await xdebug.install()
38
        }
39
40
        // TODO: Enable auto start configuration for xdebug.
41
42
        console.log('Enabling xdebug...')
43
        await xdebug.enable()
44
45
        return true
46
    }
47
48
    disable = async (xdebug: Xdebug): Promise<boolean> => {
49
        if (!(await xdebug.isInstalled())) {
50
            console.log('Extension xdebug is not installed. We do not need to disable it then...')
51
            return false
52
        }
53
54
        if (!(await xdebug.isEnabled())) {
55
            console.log('Extension xdebug is not enabled.')
56
            return false
57
        }
58
59
        await xdebug.disable()
60
61
        return true
62
    }
63
64
}
65
66
export default XdebugController